/*------------------------------------------------------------------------------

Fig A12 Bandwidth sensitivity, teacher variables II
							
------------------------------------------------------------------------------*/


global raw_data_dir="/Users/joniadamilla/Dropbox/Data/Compiled Victor/03. Productos/01. Analytic database" 

global replica_dir="<add path to your working directory>"
global data_dir=	"$replica_dir/data"
global output_dir=	"$replica_dir/output"


use "$data_dir/slvl_fin.dta", clear 


	
	
/*------------------------------------------------------------------------------
	 SCHOOL level analysis   ---   TURNOVER
*/


global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"


								 
foreach yvar of global depvars {
	foreach i in 25 10{

		rdbwselect `yvar'_tp1 st_ind`i'  				, all  c(0) p(2) kernel(uni) masspoints(off) vce(cluster rbd)  

		gen bw_mse_`yvar'_`i'=e(h_mserd)
		gen bw_cer_`yvar'_`i'=e(h_cerrd)

		rdbwselect `yvar'_tp1 st_ind`i' if municip==0 	, all   c(0) p(2) kernel(uni) masspoints(off) vce(cluster rbd)  

		gen bw_mse_`yvar'_`i'_pv=e(h_mserd)
		gen bw_cer_`yvar'_`i'_pv=e(h_cerrd)

		rdbwselect `yvar'_tp1 st_ind`i' if municip==1 	, all   c(0) p(2) kernel(uni) masspoints(off) vce(cluster rbd)  

		gen bw_mse_`yvar'_`i'_mv=e(h_mserd)
		gen bw_cer_`yvar'_`i'_mv=e(h_cerrd)

}
}

foreach i in 25 10{
	foreach y of global depvars {

	replace bw_cer_`y'_`i'=round(bw_cer_`y'_`i',0.01)
	replace bw_mse_`y'_`i'=round(bw_mse_`y'_`i',0.01)

}
}


 
la var dropped_tp1  "Turnover: Lagged attrition"
la var newtea_tp1    "Turnover: Prop. new teachers"
la var exp_tp1   	"Teacher experience"
la var aep10_tp1  	"Prop. AEP certified"

la var treat25 "Full vs. Partial Bonus"
la var treat10 "Partial vs. No Bonus"

la var st_ind25 "100% vs. 60%"
la var st_ind10 "60% vs. none"

 
save "$data_dir/temp_slvl_bw_nocovariates.dta", replace


				
/*----------------------------------------------------------------------------
	
	Regression RD - All
*/

use "$data_dir/temp_slvl_bw_nocovariates.dta", clear

global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

eststo clear


foreach i in 25 10{

local label_`i': variable label st_ind`i'
	
foreach y of global depvars {
	
		local label_`y': variable label `y'_tp1

		matrix define R = J(6, 9, .) // ( #of bw's being tested , #of statistics, missing )


		sum bw_cer_`y'_`i'
		local cer=round(r(min),0.1) // r(min) // 
		di `cer'

		sum bw_mse_`y'_`i'
		local mse=round(r(min),0.1) // r(min) // round(r(min),0.1)
		di `mse'
		
		local cer2=`cer'*2
		local mse2=`mse'*2
		
		local r = 1
		
		foreach bw in `cer'  `mse' `cer2'  `mse2'   1.5  3  {
				
			reg `y'_tp1  treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq  ${indepvars} `y'_tm1  if abs(st_ind`i')<=`bw', vce(cluster rbd)

			matrix list r(table)
			matrix beta=r(table)

			matrix R[`r', 1] =`bw'  
			matrix R[`r', 4] = beta[1,1] // rd estimate ****** used
			matrix R[`r', 5] = beta[2,1] //se_ of rd estimate ****** used
			matrix R[`r', 6] = beta[4,1] //2 * normal(-abs(R[`r', 4] / R[`r', 5])) // p-value
			matrix R[`r', 7] = beta[5,1] //R[`r', 4] - invnormal(0.975) * R[`r', 5] //CI lower
			matrix R[`r', 8] = beta[6,1] //R[`r', 4] + invnormal(0.975) * R[`r', 5] // CI upper
		 
			matrix list R
		 
			local r = `r' + 1 
		}

clear
svmat R

twoway (rcap R7 R8 R1, lcolor(navy)) ///
	(scatter R4 R1, mcolor(cranberry) yline(0, lcolor(black) lpattern(dash))), ///
	graphregion(color(white)) /// 
	xlabel( `cer'   "CER" `mse'  "MSE"  1.5 "1.5"  0 "0" `cer2' "2*CER"  `mse2' "2*MSE" 3 "3" , angle(45))  /// 
	ylabel(, angle(horizontal)) legend(off) xtitle(" ") /*xtitle("Bandwidth") ytitle("RD estimate")*/  /// 
	title(" `label_`y'', All Schools ", bmargin(0)) subtitle(" `label_`i'' ", bmargin(0))   
	
graph export "$output_dir/graph_bwsensitivity_s_`y'_tp1_`i'.png", as(png) replace


use "$data_dir/temp_slvl_bw_nocovariates.dta", clear

global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

}
}

				
				
				
/*------------------------------------------------------------------------------	

	Regression RD - private
*/


use "$data_dir/temp_slvl_bw_nocovariates.dta", clear

global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==0

	
foreach i in 25 10{

local label_`i' : variable label st_ind`i' 

foreach y of global depvars {
	
		local label_`y': variable label `y'_tp1

		matrix define R = J(6, 9, .) // ( #of bw's being tested , #of statistics, missing )


		sum bw_cer_`y'_`i'
		local cer=round(r(min),0.1) // r(min) // 
		di `cer'

		sum bw_mse_`y'_`i'
		local mse=round(r(min),0.1) // r(min) // round(r(min),0.1)
		di `mse'
		
		local cer2=`cer'*2
		local mse2=`mse'*2
		
		local r = 1
		
		foreach bw in `cer'  `mse' `cer2'  `mse2'   1.5  3  {
				
			reg `y'_tp1   treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq  ${indepvars} `y'_tm1 if abs(st_ind`i')<=`bw', vce(cluster rbd)

			matrix list r(table)
			matrix beta=r(table)

			matrix R[`r', 1] =`bw'  
			matrix R[`r', 4] = beta[1,1] // rd estimate ****** used
			matrix R[`r', 5] = beta[2,1] //se_ of rd estimate ****** used
			matrix R[`r', 6] = beta[4,1] //2 * normal(-abs(R[`r', 4] / R[`r', 5])) // p-value
			matrix R[`r', 7] = beta[5,1] //R[`r', 4] - invnormal(0.975) * R[`r', 5] //CI lower
			matrix R[`r', 8] = beta[6,1] //R[`r', 4] + invnormal(0.975) * R[`r', 5] // CI upper
		 
			matrix list R
		 
			local r = `r' + 1 
		}

clear
svmat R

twoway (rcap R7 R8 R1, lcolor(navy)) ///
	(scatter R4 R1, mcolor(cranberry) yline(0, lcolor(black) lpattern(dash))), ///
	graphregion(color(white)) /// 
	xlabel( `cer'   "CER" `mse'  "MSE"  1.5 "1.5"  0 "0" `cer2' "2*CER"  `mse2' "2*MSE" 3 "3" , angle(45))  /// 
	ylabel(, angle(horizontal)) legend(off) xtitle(" ") /*xtitle("Bandwidth") ytitle("RD estimate")*/  /// 
	title(" `label_`y'', Private Schools ", bmargin(0)) subtitle(" `label_`i'' ", bmargin(0))   
	
graph export "$output_dir/graph_bwsensitivity_s_`y'_tp1_`i'_pv.png", as(png) replace


use "$data_dir/temp_slvl_bw_nocovariates.dta", clear

global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==0

}
}






/*------------------------------------------------------------------------------	

	Regression RD - municipal
*/


use "$data_dir/temp_slvl_bw_nocovariates.dta", clear

global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==1


	
foreach i in 25 10{

local label_`i' : variable label st_ind`i' 

foreach y of global depvars {
	
		local label_`y': variable label `y'_tp1

		matrix define R = J(6, 9, .) // ( #of bw's being tested , #of statistics, missing )


		sum bw_cer_`y'_`i'
		local cer=round(r(min),0.1) // r(min) // 
		di `cer'

		sum bw_mse_`y'_`i'
		local mse=round(r(min),0.1) // r(min) // round(r(min),0.1)
		di `mse'
		
		local cer2=`cer'*2
		local mse2=`mse'*2
		
		local r = 1
		
		foreach bw in `cer'  `mse' `cer2'  `mse2'   1.5  3  {
				
			reg `y'_tp1   treat`i' st_ind`i' st_ind`i'_sq inter`i' inter`i'_sq  ${indepvars} `y'_tm1 if abs(st_ind`i')<=`bw', vce(cluster rbd)

			matrix list r(table)
			matrix beta=r(table)

			matrix R[`r', 1] =`bw'  
			matrix R[`r', 4] = beta[1,1] // rd estimate ****** used
			matrix R[`r', 5] = beta[2,1] //se_ of rd estimate ****** used
			matrix R[`r', 6] = beta[4,1] //2 * normal(-abs(R[`r', 4] / R[`r', 5])) // p-value
			matrix R[`r', 7] = beta[5,1] //R[`r', 4] - invnormal(0.975) * R[`r', 5] //CI lower
			matrix R[`r', 8] = beta[6,1] //R[`r', 4] + invnormal(0.975) * R[`r', 5] // CI upper
		 
			matrix list R
		 
			local r = `r' + 1 
		}

clear
svmat R

twoway (rcap R7 R8 R1, lcolor(navy)) ///
	(scatter R4 R1, mcolor(cranberry) yline(0, lcolor(black) lpattern(dash))), ///
	graphregion(color(white)) /// 
	xlabel( `cer'   "CER" `mse'  "MSE"  1.5 "1.5"  0 "0" `cer2' "2*CER"  `mse2' "2*MSE" 3 "3" , angle(45))  /// 
	ylabel(, angle(horizontal)) legend(off) xtitle(" ") /*xtitle("Bandwidth") ytitle("RD estimate")*/  /// 
	title(" `label_`y'', Municipal Schools ", bmargin(0)) subtitle(" `label_`i'' ", bmargin(0))   
	
graph export "$output_dir/graph_bwsensitivity_s_`y'_tp1_`i'_mv.png", as(png) replace


use "$data_dir/temp_slvl_bw_nocovariates.dta", clear

global depvars  "dropped newtea exp aep10" 
global indepvars "municip_tm1 rural_rbd_tm1 gender_tm1 tit_educ_tm1 permanent_tm1 tea_admin_tm1 multsch_tm1 i.sned_yr i.hg"

keep if municip==1

}
}

				
erase  "$data_dir/temp_slvl_bw_nocovariates.dta"
